package com.teligen.zkdb.service;


import com.teligen.zkdb.db.DbConfig;
import com.teligen.zkdb.db.DruidUtil;
import com.teligen.zkdb.zk.DataChange;
import com.teligen.zkdb.zk.ZookeeperUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

@Component
public class DbDataRegister implements CommandLineRunner {
    @Value("${zk.host}")
    private String zkHost;

    @Autowired
    private ZookeeperUtil zookeeperUtil;
    @Override
    public void run(String... args) {
        DbConfig dbConfig = zookeeperUtil.readDbConfig();
        DruidUtil.createNewConnect(dbConfig);

        zookeeperUtil.watchDbConfig(new DataChange() {
            @Override
            public void dataChange(String s, Object obj) {
                DbConfig dbConfig1 = (DbConfig) obj;
                DruidUtil.switchConnect(dbConfig1);
            }
        });
    }
}
